#!/bin/bash

# 大模型集成功能测试脚本
# 用于测试MCP Client与大模型的集成功能

BASE_URL="http://localhost:8080"
SESSION_ID="test-session-$(date +%s)"

echo "=== BoulderAI MCP Client - 大模型集成功能测试 ==="
echo "Base URL: $BASE_URL"
echo "Session ID: $SESSION_ID"
echo ""

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 测试函数
test_api() {
    local name="$1"
    local method="$2"
    local url="$3"
    local data="$4"
    
    echo -e "${BLUE}测试: $name${NC}"
    echo "请求: $method $url"
    
    if [ -n "$data" ]; then
        response=$(curl -s -w "\n%{http_code}" -X "$method" "$url" \
            -H "Content-Type: application/json" \
            -d "$data")
    else
        response=$(curl -s -w "\n%{http_code}" -X "$method" "$url")
    fi
    
    http_code=$(echo "$response" | tail -n1)
    body=$(echo "$response" | head -n -1)
    
    if [ "$http_code" -eq 200 ]; then
        echo -e "${GREEN}✓ 成功 (HTTP $http_code)${NC}"
        echo "响应: $body" | head -c 200
        if [ ${#body} -gt 200 ]; then
            echo "..."
        fi
    else
        echo -e "${RED}✗ 失败 (HTTP $http_code)${NC}"
        echo "响应: $body"
    fi
    echo ""
}

# 1. 测试应用健康状态
echo -e "${YELLOW}1. 检查应用状态${NC}"
test_api "应用健康检查" "GET" "$BASE_URL/actuator/health"

# 2. 测试获取支持的大模型客户端类型
echo -e "${YELLOW}2. 获取支持的大模型客户端类型${NC}"
test_api "获取客户端类型" "GET" "$BASE_URL/api/llm/clients"

# 3. 测试获取OpenAI支持的模型
echo -e "${YELLOW}3. 获取OpenAI支持的模型${NC}"
test_api "获取OpenAI模型" "GET" "$BASE_URL/api/llm/clients/openai/models"

# 4. 测试OpenAI客户端健康检查（需要API密钥）
echo -e "${YELLOW}4. 测试OpenAI客户端健康检查${NC}"
if [ -n "$OPENAI_API_KEY" ]; then
    test_api "OpenAI健康检查" "GET" "$BASE_URL/api/llm/clients/openai/health?apiKey=$OPENAI_API_KEY"
else
    echo -e "${YELLOW}跳过 - 未设置OPENAI_API_KEY环境变量${NC}"
    echo ""
fi

# 5. 测试MCP连接状态
echo -e "${YELLOW}5. 测试MCP连接状态${NC}"
test_api "MCP连接状态" "GET" "$BASE_URL/api/mcp/status/$SESSION_ID"

# 6. 测试MCP服务器连接（如果有本地MCP服务器）
echo -e "${YELLOW}6. 测试MCP服务器连接${NC}"
MCP_SERVER_URI="ws://localhost:3001"
connect_data='{"serverUri": "'$MCP_SERVER_URI'"}'
test_api "连接MCP服务器" "POST" "$BASE_URL/api/mcp/connect" "$connect_data"

# 7. 测试大模型对话（需要API密钥和MCP连接）
echo -e "${YELLOW}7. 测试大模型对话功能${NC}"
if [ -n "$OPENAI_API_KEY" ]; then
    chat_data='{
        "clientType": "openai",
        "model": "gpt-3.5-turbo",
        "messages": [
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "Hello! Please introduce yourself."
            }
        ],
        "clientConfig": {
            "apiKey": "'$OPENAI_API_KEY'"
        }
    }'
    test_api "大模型对话" "POST" "$BASE_URL/api/llm/chat/$SESSION_ID" "$chat_data"
else
    echo -e "${YELLOW}跳过 - 未设置OPENAI_API_KEY环境变量${NC}"
    echo ""
fi

# 8. 清理：断开MCP连接
echo -e "${YELLOW}8. 清理资源${NC}"
test_api "断开MCP连接" "POST" "$BASE_URL/api/mcp/disconnect/$SESSION_ID"

echo -e "${GREEN}=== 测试完成 ===${NC}"
echo ""
echo "使用说明："
echo "1. 确保应用正在运行: mvn spring-boot:run"
echo "2. 设置OpenAI API密钥: export OPENAI_API_KEY='your-key'"
echo "3. 启动本地MCP服务器（可选）: ./start-mcp-server.sh"
echo "4. 运行此测试脚本: ./test-llm-integration.sh"
echo ""
echo "更多信息请参考: README-LLM-INTEGRATION.md"